﻿using System.Security.Claims;
using AutoMapper;
using BlogSystem.Common.Config.DI.annotation;
using BlogSystem.Data.Entity;
using BlogSystem.Data.Repository.RepositoryImpl;
using BlogSystem.Exception;
using BlogSystem.Model.Request;
using BlogSystem.Model.Respones;

namespace BlogSystem.Service.Impl;

[Ioc("scoped")]
public class ArticleServiceImpl : IArticleService
{

    private readonly IMapper _mapper;
    
    private readonly IHttpContextAccessor _httpContextAccessor;

    private readonly ArticleRepository _articleRepository;
    public ArticleServiceImpl(IMapper mapper,IHttpContextAccessor httpContextAccessor,ArticleRepository articleRepository)
    {
        _mapper = mapper;
        _httpContextAccessor = httpContextAccessor;
        _articleRepository =  articleRepository;
    }

    /// <summary>
    /// 分页+查询
    /// </summary>
    /// <param name="keyWord">关键词</param>
    /// <param name="pageIndex">页码</param>
    /// <param name="pageSize">页面大小</param>
    /// <returns></returns>
    public async Task<IQueryable<ArticleResponse>> GetArticlesAsync(string keyWord,int pageIndex,int pageSize)
    {
        return await _articleRepository.GetAllByPage( keyWord, pageIndex, pageSize);
    }

    /**
     * 添加
     */
    public async Task<bool> AddArticle(GetArticleRequest getArticleRequest)
    {
        // 获取到用户的信息
        var user = _httpContextAccessor.HttpContext?.User;
        var userid = user.FindFirstValue("id");
        
        var article = _mapper.Map<Article>(getArticleRequest);
        article.UserId = int.Parse(userid);
        
        //await  _context.ArticlesParameter.AddAsync(article);
        var res= await _articleRepository.Add(article);
        return res;
    }

    public async Task<bool> UpdateArticle(int id,GetArticleRequest articleRequest)
    {
        //获取id,查找出对应的数据库Article
        var article = await _articleRepository.GetById(id);
        if (article==null)
             return false;
        
        article = _mapper.Map(articleRequest,article);
        var res = await _articleRepository.Update(article);
        return res;
    }
    
    /**
     * 评论id 获取对应信息
     */
    public async Task<Article> GetById(int id)
    {
        var article = await _articleRepository.GetById(id);
        if (article.Status!=1)
        {
            throw new BusinessException("该文章已被屏蔽。");
        }
        return article;
      
    }

    /**
     * 点赞
     */
    public async Task<bool> AddLikeById(int id)
    {
        var res = await _articleRepository.AddLikeById(id);
        return res;
        
    }

    /**
     * 收藏
     */
    public async Task<bool> AddSaveById(int id)
    {
        var res = await _articleRepository.AddSaveById(id);
        return res;
    }

    /**
     * 阅读
     */
    public async Task<bool> AddReadById(int id)
    { 
        var res = await _articleRepository.AddReadById(id);
        return res;
    }

    /**
     * 逻辑删除
     */
    public async Task<bool> DelArticleById(int id)
    {
        var res =await _articleRepository.DelArticleById(id);
        return res;
        
    }
}